package EMS::PhoneHome;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);


@ISA = qw(Exporter AutoLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
	new
	PhoneHome
);
$VERSION = '$Id: PhoneHome.pm,v 1.1 2002/12/13 18:10:41 djohnsen Exp $';

my $DEFAULT_TO 			= "ems-duty\@demodomain.com";
my $DEFAULT_MAIL_FROM 	= "ems-duty\@demodomain.com";

# Preloaded methods go here.

sub new {
	shift;
	my $self = {};
	bless($self);
	# Create a sensible quoted sender line containing the calling script
	# and the hostname so it will be obvious to the recipient where this
	# message came from
	my $script = "$0";			
	$script =~ s,^.*/,,;		# strip off leading path
	my $hostname = `hostname`;	# get hostname part to include in quoted sender
	chomp $hostname;
	my $default_from = "$script\@$hostname";

	my %DEFAULTS = (
		TO			=>	"$DEFAULT_TO",
		FROM		=>	"$default_from",
		MAIL_FROM	=>	"$DEFAULT_MAIL_FROM",
    	@_					 	# pulls in options set on the fly by caller
	);
	# Does key substitution against defaults with caller-provided values
	for(sort(keys %DEFAULTS)) {
		$self->{$_} = $DEFAULTS{$_};
	}
	return $self;
}

sub PhoneHome {
    my $self = shift();
	my $to = $self->{TO};
	my $from = $self->{FROM};
	my $mail_from = $self->{MAIL_FROM};
	my ($subject, $message) = @_;
	open(MAIL, "|/usr/lib/sendmail -f$mail_from $to");
	print MAIL "To: $to\n";
	print MAIL "From: \"$from\" <$mail_from>\n";
	print MAIL "Subject: $subject\n";
	print MAIL "X-mailer: EMS::PhoneHome $VERSION\n";
	print MAIL "\n";
	print MAIL "$message\n";
}


1;
__END__

=head1 NAME

EMS::PhoneHome - mechanism to send intelligently-addressed mail from EMS scripts

=head1 SYNOPSIS

  use EMS::PhoneHome;

# example of a plain vanilla mail using defaults

  # default TO=ems-duty, FROM=script@host
  my $notify = EMS::PhoneHome->new();	
  $notify->PhoneHome("Some Subject", "Some Message");

# example of mail sent to a specific address, resetting the 
# FROM, and sending only a subject

  my $page = EMS::PhoneHome->new(
	TO	=>	"ems-duty\@epage.demodomain.com",
	FROM 	=>	"Some Arbitrary String but not an email address"
	);
  $page->PhoneHome("Just a Subject");

=head1 DESCRIPTION

Creates a mail message using certain intelligent defaults appropriate to EMS scripts.

  TO 		= 	ems-duty@demodomain.com
  FROM 		= 	<name of script>@<hostname>
  MAIL_FROM	=	ems-duty@demodomain.com   (in case of bounces)

=head1 AUTHOR

Don Johnsen
Modified by Steven G. Harms ( stharms@cisco.com ) 

=cut
